package com.varian.auth.service.impl;

import cn.hutool.captcha.GifCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.varian.auth.model.dto.OAuth2CaptchaDTO;
import com.varian.auth.service.IOAuth2Service;
import com.varian.auth.util.CaptchaUtil;
import org.springframework.stereotype.Service;

import java.awt.*;

/**
 * @author ben
 * @since 2024/7/26
 */
@Service
public class OAuth2ServiceImpl implements IOAuth2Service {

    private final RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);

    @Override
    public String captcha(OAuth2CaptchaDTO dto) {
        GifCaptcha gifCaptcha = new GifCaptcha(dto.getWidth(), dto.getHeight());
        if (Boolean.TRUE.equals(dto.getDarkTheme())) {
            gifCaptcha.setBackground(Color.DARK_GRAY);
            gifCaptcha.setMinColor(127);
        } else {
            gifCaptcha.setBackground(Color.WHITE);
            gifCaptcha.setMaxColor(127);
        }
        gifCaptcha.setGenerator(randomGenerator);
        gifCaptcha.createCode();
        CaptchaUtil.cacheCode(dto.getUuid(), gifCaptcha.getCode());
        return gifCaptcha.getImageBase64Data();
    }
}
